import { createStore } from 'vuex'

// 合并歌曲到播放列表查重
const concatPlayList = (list: Array<any>, playList = []) => {
  // filter过滤无版权及vip歌曲
  const arr = [
    ...playList,
    ...list.filter((item) => {
      return !item.license && item.fee != 1
    }),
  ]
  const map = new Map()
  for (const item of arr) {
    if (!map.has(item.id)) {
      map.set(item.id, item)
    }
  }
  return [...map.values()]
}
// 当前歌曲在播放列表的索引
const findIndex = (list: any, playList: Array<any>) => {
  const index = playList.findIndex((d) => {
    return d.id === list.id
  })
  return index !== -1 ? index : 0
}
export default createStore({
  state: {
    isLogin: false, // 登录状态
    isPlayed: false, // 当前播放状态
    playList: [], // 播放列表
    playIndex: 0, // 当前播放歌曲在播放列表的所有位置
    isShowPlayListTips: false, // 播放成功后，播放列表按钮提示的文字
    isShowAddPlayListTips: false // 添加后，播放列表按钮提示的文字
  },
  mutations: { 
    SET_ISLOGIN(state, val = false) {
      state.isLogin = val
    },
    SET_PLAYSTATUS(state, val = false) {
      state.isPlayed = val
    },
    SET_PLAYLIST(state, val = null) {
      state.playList = val
      window.localStorage.setItem('playList', JSON.stringify(val))
    },
    SET_PLAYINDEX(state, val = 0) {
      state.playIndex = val
      window.localStorage.setItem('playIndex', val)
    },
    SET_PLAYLISTTIPS(state, val = false) {
      state.isShowPlayListTips = val
    },
    SET_ADDPLAYLISTTIPS(state, val = false) {
      state.isShowAddPlayListTips = val
    },
  },
  actions: {
    // 播放歌曲列表里全部歌曲（清空当前播放列表）
    playAll({ commit }, { list }) {
      commit('SET_PLAYLIST', concatPlayList(list))
      commit('SET_PLAYSTATUS', true)
      commit('SET_PLAYINDEX', 0)
    },
    // 播放当前选中的歌曲
    selectPlay({ commit, state }, { list }) {
      const playList = concatPlayList(list, state.playList)
      commit('SET_PLAYLIST', playList)
      commit('SET_PLAYSTATUS', true)
      commit('SET_PLAYINDEX', findIndex(list[0], playList))
      commit('SET_PLAYLISTTIPS', true)
    },
    // 添加歌曲到当前播放列表
    addList({ commit, state }, { list }) {
      const playList = concatPlayList(list, state.playList)
      commit('SET_PLAYLIST', playList)
      commit('SET_ADDPLAYLISTTIPS', true)
    },
    // 删除播放列表的歌曲
    delList({ commit, state }, { list }) {
      if(list.length == 0) {
        commit('SET_PLAYSTATUS', false)
      }
      commit('SET_PLAYINDEX', findIndex(state.playList[state.playIndex], list))
      commit('SET_PLAYLIST', list)
    }
  },
  modules: {},
  getters: {
    isLogin(state) {
      return state.isLogin
    },
    isPlayed(state) {
      return state.isPlayed
    },
    playList(state) {
      return state.playList.length
        ? state.playList
        : JSON.parse(localStorage.getItem('playList') as string) || []
    },
    playIndex(state) {
      return (
        state.playIndex ||
        JSON.parse(localStorage.getItem('playIndex') as string) ||
        0
      )
    },
    isShowPlayListTips(state) {
      return state.isShowPlayListTips
    },
    isShowAddPlayListTips(state) {
      return state.isShowAddPlayListTips
    },
  },
})
